<template>
  <div class="fr page">
    <div class="sui-pagination clearfix">
      <ul>
        <li>
          <button
            class="prev"
            :disabled="pageNo === 1"
            @click="$emit('getPageNo', pageNo - 1)"
          >
            «上一页
          </button>
        </li>

        <!-- 前 -->
        <li
          v-if="startNumAndendNum.start > 1"
          @click="$emit('getPageNo', 1)"
          :class="{ active: pageNo === 1 }"
        >
          <a href="#">1</a>
        </li>
        <li class="dotted" v-if="startNumAndendNum.start > 2">
          <span>...</span>
        </li>

        <!-- 中 -->
        <li
          v-for="(page, index) in startNumAndendNum.end"
          v-show="page >= startNumAndendNum.start"
          :class="{ active: pageNo === page }"
          :key="index"
          @click="$emit('getPageNo', page)"
        >
          <a href="#">{{ page }}</a>
        </li>

        <!-- 后 -->
        <li class="dotted" v-if="startNumAndendNum.end < totalPage - 1">
          <span>...</span>
        </li>
        <li
          :class="{ active: pageNo === totalPage }"
          v-if="startNumAndendNum.end < totalPage"
          @click="$emit('getPageNo', totalPage)"
        >
          <a href="#">{{ totalPage }}</a>
        </li>

        <li>
          <button
            class="next"
            @click="$emit('getPageNo', pageNo + 1)"
            :disabled="pageNo === totalPage"
          >
            下一页»
          </button>
        </li>
      </ul>
      <div>
        <span>共 {{ total }} 条</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "PaginationList",
  props: ["pageNo", "pageSize", "total", "continues"],
  computed: {
    // 总页数
    totalPage() {
      return Math.ceil(this.total / this.pageSize);
    },
    startNumAndendNum() {
      let start = 0;
      let end = 0;
      const { continues, totalPage, pageNo } = this;
      if (continues > totalPage) {
        start = 1;
        end = totalPage;
      } else {
        start = pageNo - Math.floor(continues / 2);
        end = pageNo + parseInt(continues / 2);
        if (start < 1) {
          start = 1;
          end = continues;
        }
        if (end > totalPage) {
          start = totalPage - continues + 1;
          end = totalPage;
        }
      }
      return { start, end };
    },
  },
};
</script>

<style lang='less' scoped>
.page {
  width: 700px;
  margin-left: 100px;
  // float: right;

  .sui-pagination {
    margin: 15px 0;

    ul {
      float: left;

      li {
        display: inline-block;

        a {
          float: left;
          width: 40px;
          text-align: center;
          padding: 10px 0px;
          text-decoration: none;
          border: 1px solid #e0e9ee;
          margin: 0px 0px 0px -1px;
          font-size: 14px;
          color: #333;
        }

        &.active {
          background-color: #e1251b;
          a {
            color: #ffffff;
          }
        }

        button {
          float: left;
          width: 90px;
          height: 40px;
          line-height: 40px;
          background-color: #fafafa;
          border: 1px solid #e0e9ee;
          font-size: 14px;
          cursor: pointer;

          &.prev {
            margin: 0px;
          }

          &.next {
            margin: 0px 0px 0px -1px;
          }
        }

        button:disabled {
          cursor: not-allowed;
          color: #e0e9ee;
        }

        &.dotted {
          span {
            width: 40px;
            text-align: center;
            text-decoration: none;
            font-size: 14px;
            padding: 11px 0px;
            color: #333;
            float: left;
          }
        }
      }
    }

    div {
      color: #333;
      font-size: 14px;
      float: left;
      width: 130px;
      height: 38px;
      line-height: 38px;
      text-align: center;
    }
  }
}
</style>